package ee.metadataUtils.impl;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Type;

import ee.metadataUtils.PropertyMetadataAdapterFactoryRegistry;

public class FieldPropertyMetadata extends AbstractPropertyMetadata {

	private Field _field;
	private String _name;
	private Type _genericType;
	private Class<?> _type;
	private Annotation[] _annotations;
	
	public FieldPropertyMetadata(PropertyMetadataAdapterFactoryRegistry adapterFactoryRegistry, Field field)
	{
		super(adapterFactoryRegistry);
		
		_field = field;
	}
	
	@Override
	public String getName() {
		if (_name == null)
		{
			_name = _field.getName();
		}
		return _name;
	}

	@Override
	public Type getGenericType() {
		
		if (_genericType == null)
		{
			_genericType = _field.getGenericType();
		}
		
		return _genericType;
	}

	@Override
	public void setValue(Object instance, Object value) throws Throwable {
		_field.set(instance, value);
	}

	@Override
	public Object getValue(Object instance) throws Throwable {
		return _field.get(instance);
	}
	
	@Override
	public Class<?> getType() {
		if (_type == null)
		{
			_type = _field.getType();
		}
		
		return _type;
	}


	@Override
	public Annotation[] getAnnotations() {
		
		if (_annotations == null)
		{
			_annotations = _field.getAnnotations();
		}
		
		return _annotations;
	}
}
